using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;

namespace PrinterHack
{
    class Printer
    {
        #region Variables
        
        #endregion

        #region Public Properties
        private string guid = Guid.NewGuid().ToString();
        public string GUID
        {
            get { return guid; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string ip;
        public string IP
        {
            get { return ip; }
            set { ip = value; }
        }

        private string description;
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        private bool isNew = true;
        public bool IsNew
        {
            get { return isNew; }
        }

        private DataSet printers = new DataSet("printer");

        public static DataSet PrinterData
        {
            get
            {
                try
                {
                    DataSet ds = new DataSet("printer");
                    ds.ReadXml(frmMain.DataFile);
                    return ds;
                }
                catch { return new DataSet(); }
            }
        }
        #endregion

        #region Constructor
        public Printer()
        {
            LoadPrinterData();
        }

        public Printer(string PrinterID)
        {
            LoadPrinterData();
            DataTable printerTable = printers.Tables["printer"];

            foreach (DataRow row in printerTable.Rows)
            {
                if (row["id"].ToString() == PrinterID)
                {
                    this.guid = row["id"].ToString();
                    this.Name = row["name"].ToString();
                    this.IP = row["ip"].ToString();
                    this.Description = row["description"].ToString();
                    this.isNew = false;
                    break;
                }
            }
        }

        #endregion

        #region Custom Methods
        private void LoadPrinterData()
        {
            try
            {
                printers.ReadXml(frmMain.DataFile);
                DataColumn[] columns = new DataColumn[1];
                columns[0] = printers.Tables["printer"].Columns["id"];
                printers.Tables["printer"].PrimaryKey = columns;
                printers.Tables["printer"].Columns["name"].Unique = true;
                printers.Tables["printer"].Columns["ip"].Unique = true;
            }
            catch { }
        }

        public void Save()
        {            
            DataTable printerTable = printers.Tables["printer"];        
            
            if (this.isNew)
            {
                DataRow row = printerTable.NewRow();
                row["id"] = this.GUID;
                row["name"] = this.Name;
                row["ip"] = this.IP;
                row["description"] = this.Description;
                printerTable.Rows.Add(row);                    
            }
            else
            {
                int index = 0;
                foreach (DataRow row in printerTable.Rows)
                {
                    if (row["id"].ToString() == this.GUID)
                    {
                        printerTable.Rows[index]["name"] = this.Name;
                        printerTable.Rows[index]["ip"] = this.IP;
                        printerTable.Rows[index]["description"] = this.Description;
                        break;
                    }
                    index++;
                }
            }

            printers.WriteXml(frmMain.DataFile);
        }

        public void Delete()
        {
        }

        public void LoadDataByName(string Name)
        {
            DataTable printerTable = printers.Tables["printer"];

            foreach (DataRow row in printerTable.Rows)
            {
                if (row["name"].ToString() == Name)
                {
                    this.guid = row["id"].ToString();
                    this.Name = row["name"].ToString();
                    this.IP = row["ip"].ToString();
                    this.Description = row["description"].ToString();
                    this.isNew = false;
                    break;
                }
            }
        }
        #endregion
    }
}
